<template>
	<view>
		<!-- pages/Cash/index.wxml -->
		<topBar text="提现" :needBack="true"></topBar>
		<view class="head-block" v-if="weixin == 2">
			<view class="nav-title" :style="'top:10px'"></view>
			<!-- 返回 -->
			<view class="icon" @tap="goMy" :style="'top:6px'">
				<van-icon name="arrow-left" color="#333" size="45rpx" />
			</view>
		</view>
		<view class="body-container">
			<view>
				<view class="body-t">
					<view class="body-t-t1">¥{{ balance }}</view>
					<view class="body-t-t2">可提现金额</view>
				</view>
				<view class="body-b">
					<van-cell-group>
						<view class="topTitle">银行卡号</view>
						<input class="topContent" v-model="bankCardNum" placeholder="请输入" />
					</van-cell-group>

					<van-cell-group>
						<view class="topTitle">开户行</view>
						<input class="topContent" v-model="bankAccount" placeholder="请输入" :border="false" />
					</van-cell-group>
					<van-cell-group>
						<view class="topTitle">姓名</view>
						<input class="topContent" v-model="name" placeholder="请输入" :border="false" />
					</van-cell-group>
					<van-cell-group>
						<view class="topTitle">身份证号</view>
						<input class="topContent" v-model="idCard" type="idcard" placeholder="请输入" :border="false" />
					</van-cell-group>
					<view class="tips">温馨提示：请填写银行卡开户人姓名</view>
				</view>
				<view class="btn">
					<van-button type="info" @tap.native="goOK">确定提现</van-button>
				</view>
			</view>
			<van-dialog use-slot title="信息确认" v-model="show" show-cancel-button confirm-button-open-type="getUserInfo"
				@close="onClose" @getuserinfo="getUserInfo" confirm-button-color="#00BCAB" confirm-button-text="确认提现">
				<view class="tips popup-tips">提交后不能修改，请确认卡号信息</view>
				<view class="popup-body">
					<view class="popup-t1">银行卡</view>
					<view class="popup-t2">{{ bankCardNum }}</view>
				</view>
				<view class="popup-body">
					<view class="popup-t1">开户行</view>
					<view class="popup-t2">{{ bankAccount }}</view>
				</view>
				<view class="popup-body">
					<view class="popup-t1">持卡人</view>
					<view class="popup-t2">{{ name }}</view>
				</view>
				<view class="popup-body">
					<view class="popup-t1">身份证号</view>
					<view class="popup-t2">{{ idCard }}</view>
				</view>
			</van-dialog>
			<van-dialog id="van-dialog" confirm-button-color="#00BCAB" confirm-button-text="我知道了" />
		</view>
	</view>
</template>

<script>
	// pages/Cash/index.js
	import util from './../../utils/util.js';
	import api from './../../utils/api.js';
	import {
		Dialog
	} from 'vant';
	export default {
		data() {
			return {
				show: false,
				bankCardNum: '',
				name: '',
				bankAccount: '',
				balance: '',
				idCard: '',
				allMoney: '',
				withdrawalMoney: '',
				weixin:uni.getStorageSync('weixin')
			};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad: function(options) {
			const unionId = uni.getStorageSync('unionId');
			this.getmyInCome(unionId);
		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		// onShareAppMessage: function() {}
		onReachBottom: function() {},
		/**
		 * 用户点击右上角分享
		 */
		methods: {
			goMy() {
				uni.navigateBack({
					delta: 1
				});
			},
			goOK() {
				if (this.bankCardNum == '') {
					uni.showToast({
						title: '请输入银行卡号',
						icon: 'none'
					});
				} else if (this.name == '') {
					uni.showToast({
						title: '请输入银行卡号',
						icon: 'none'
					});
				} else if (this.bankAccount == '') {
					uni.showToast({
						title: '请输入银行卡号',
						icon: 'none'
					});
				} else {
					this.setData({
						show: true
					});
				}
			},

			getUserInfo(event) {
				this.getwithdrawal();
			},

			onClose() {
				this.setData({
					show: false
				});
			},

			getwithdrawal() {
				const amount = this.balance; // wx.getStorageSync("balance")

				const unionId = uni.getStorageSync('unionId');
				const phoneNumber = uni.getStorageSync('phoneNumber');
				const identityId = uni.getStorageSync('identityId');
				const param = {
					bankCardNum: this.bankCardNum,
					bankAccount: this.bankAccount,
					name: this.name,
					unionId: unionId,
					phone: phoneNumber,
					identityId: identityId,
					amount: amount
				};
				util.request(api.withdrawal, param, 'POST').then((res) => {
					if (res.success) {
						Dialog.alert({
							title: '信息确认',
							message: '申请已提交，请耐心等待'
						}).then((res) => {
							uni.switchTab({
								url: '../home/index'
							});
						});
					} else {
						uni.showToast({
							title: res.msg || '接口出错了，请稍后再试',
							icon: 'none'
						});
					}
				});
			},

			//我的钱包
			getmyInCome(unionId) {
				util.request(api.getMyInCome + '?unionId=' + unionId, 'get').then((res) => {
					if (res.code == 0) {
						const allMoney = res.data.allMoney ? res.data.allMoney / 100 : 0;
						const withdrawalMoney = res.data.withdrawalMoney ? res.data.withdrawalMoney / 100 : 0;
						const balance = res.data.balance ? res.data.balance / 100 :
						0; // const balance = res.data.allMoney ? +res.data.allMoney / 100 : 0;

						this.setData({
							allMoney: allMoney,
							withdrawalMoney: withdrawalMoney,
							balance: balance
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				});
			}
		}
	};
</script>
<style>
	@import './index.css';
</style>
